﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;



public partial class Front_UserControl_uxSignup : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {
        //setSecureProtocol(true);
    }

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) {


        AddUser1();
    }


    public void setSecureProtocol(bool bSecure) {
        string redirectUrl = null;

        // if we want HTTPS and it is currently HTTP
        if (bSecure && !Request.IsSecureConnection)
            redirectUrl = Request.Url.ToString().Replace("http:", "https:");

        else

            // if we want HTTP and it is currently HTTPS
            if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

        //else

        // in all other cases we don't need to redirect

        // check if we need to redirect, and if so use redirectUrl to do the job
        if (redirectUrl != null)

            Response.Redirect(redirectUrl);

    }
    protected void AddUser1() {

        TextBox txtName =
            (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtName");
        TextBox Password =
          (TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Password");
        TextBox UserName =
          (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
        CheckBox chkPolicy =
                 (CheckBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("chkPolicy");
        Literal Msg =
                (Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtError");

        try {

            MembershipUser user = Membership.GetUser(UserName.Text);
            user.IsApproved = true;
            user.Email = UserName.Text;
            Membership.UpdateUser(user);
            var UserID = user.ProviderUserKey;
            Session["username"] = UserName.Text;
            System.Web.Security.Roles.AddUserToRole(UserName.Text, "user");
            FormsAuthentication.SetAuthCookie(UserName.Text, false);
            //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
            //replacements.Add("<%UserName%>", UserName.Text);
            //replacements.Add("<%FullName%>", txtName.Text);
            //replacements.Add("<%Email%>", UserName.Text);
            //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl, Mailer.MessageTemplate.NewUserRegistrationConfirmation, replacements, UserName.Text, "Your Zupons Registration");
            //Mailer.InternalSend(confirmation, false);
            string strData = txtName.Text;
            char[] splitChar = new char[1];
            splitChar[0] = ' ';
            String[] col = strData.Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
            string FName = col[0];
            string LName = "";
            if (col.Count() > 1) {
                FName = col[0];
                LName = col[1];
            }
            string Address = null;
            string City = null;
            string State = null;
            string ZipCode = null;
            string Phone_Home = null;
            System.Data.Linq.Binary UserImage = null;
            string Phone_Mob = null;
            string Phone_Off = null;
            string Fax = null;
            string CardHolderName = null;
            string CardNumber = null;
            string BillingAddress = null;
            string SecurityCode = null;
            string City2 = null;
            string State2 = null;
            string ExpirationDate = "1/1/1900 12:00:00 AM";
            string PostalCode = null;
            bool Policy = true;
            var reg = new DealesDataContext();
            var RegisterQuery = reg.Insert_UserDetails(Convert.ToString(UserID), FName, LName, Address, City, State, ZipCode, UserImage, Phone_Home, Phone_Mob, Phone_Off, Fax, Convert.ToString(UserID), CardHolderName, Convert.ToInt64(CardNumber), BillingAddress, Convert.ToInt64(SecurityCode), City2, State2, Convert.ToDateTime(ExpirationDate), PostalCode, Policy);
            if (Session["ReferUserID"] != null) {
                var ReferUser = reg.Insert_ReferUser(new Guid(Convert.ToString(UserID)), new Guid(Convert.ToString(Session["ReferUserID"])), Convert.ToInt32(Session["DealID"]));

            }
            Response.Redirect(@"~\MyAccountSettings.aspx");
        } catch (MembershipCreateUserException e) {
            Msg.Text = e.Message;
        } catch (HttpException e) {
            Msg.Text = e.Message;
        }

    }



    protected void CHECKBOX(object source, ServerValidateEventArgs args) {
        CheckBox chkPolicy =
                (CheckBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("chkPolicy");
        Literal Msg =
          (Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtError");

        if (chkPolicy.Checked == false) {

            Msg.Text = "You must agree to Terms of Use";
            args.IsValid = false;
            return;
        }
    }

    protected void CreateUserWizard1_CreateUserError(object sender, CreateUserErrorEventArgs e) {
        Literal txtError =
                (Literal)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtError");

        switch (e.CreateUserError) {
            case MembershipCreateStatus.DuplicateUserName:
                txtError.Text = "User name already exists. Please sign in or enter a different user name.";
                break;

            case MembershipCreateStatus.DuplicateEmail:
                txtError.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
                break;

            case MembershipCreateStatus.InvalidPassword:
                txtError.Text = "The password provided is invalid. Please enter a valid password value.";
                break;

            case MembershipCreateStatus.InvalidEmail:
                txtError.Text = "The e-mail address provided is invalid. Please check the value and try again.";
                break;

            case MembershipCreateStatus.InvalidAnswer:
                txtError.Text = "The password retrieval answer provided is invalid. Please check the value and try again.";
                break;

            case MembershipCreateStatus.InvalidQuestion:
                txtError.Text = "The password retrieval question provided is invalid. Please check the value and try again.";
                break;

            case MembershipCreateStatus.InvalidUserName:
                txtError.Text = "The user name provided is invalid. Please check the value and try again.";
                break;

            case MembershipCreateStatus.ProviderError:
                txtError.Text = "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                break;

            case MembershipCreateStatus.UserRejected:
                txtError.Text = "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                break;

            default:
                txtError.Text = "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                break;
        }
    }
}
